package com.yinhe.music.yhmusic.share;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.hwangjr.rxbus.RxBus;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMusic;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusListObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.base.IBaseView;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;

public class SharePresenter<M extends IBaseModel, V extends IBaseView> extends BasePresenter<M, V> implements ISharePresenter {

    @Override
    public void getShareInfo(String shareType, int id, Activity activity) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getShareInfo(id, shareType)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(shareInfoResponse -> {
                    parseShareInfo(shareInfoResponse, shareType, activity);
                }, throwable -> {
                    getView().showMessage(throwable);
                }));
    }

    private void parseShareInfo(ShareInfoResponse response, String shareType, Activity activity) {
        if (shareType.equals(IConstants.SONG) || shareType.equals(IConstants.RADIO_SONG)) {
            if (shareType.equals(IConstants.SONG)) {
                UmengEventUtils.clickShare(activity, UmengEventUtils.SONG);
            } else {
                UmengEventUtils.clickShare(activity, UmengEventUtils.RAIDO_SONG);
            }

            UMusic uMusic = new UMusic(response.shareInfo.getPlayUrl());
            UMImage image;
            if (!TextUtils.isEmpty(response.shareInfo.shareImage)) {
                image = new UMImage(MusicApplication.getContext(), response.shareInfo.shareImage);
            } else {
                image = new UMImage(MusicApplication.getContext(), R.drawable.default_cover);
            }

            image.compressStyle = UMImage.CompressStyle.SCALE;
            uMusic.setTitle(response.shareInfo.shareTitle);
            uMusic.setmTargetUrl(response.shareInfo.shareUrl);
            uMusic.setThumb(image);
            uMusic.setDescription(response.shareInfo.shareContent);
            share(activity, uMusic, null);
        } else {
            UmengEventUtils.clickShare(activity, UmengEventUtils.MV);
            UMVideo umVideo = new UMVideo(response.shareInfo.getPlayUrl());
            UMImage image;
            if (!TextUtils.isEmpty(response.shareInfo.shareImage)) {
                image = new UMImage(MusicApplication.getContext(), response.shareInfo.shareImage);
            } else {
                image = new UMImage(activity, R.drawable.ic_default_mv);
            }
            image.compressStyle = UMImage.CompressStyle.SCALE;
            umVideo.setTitle(response.shareInfo.shareTitle);
            umVideo.setThumb(image);
            umVideo.setDescription(response.shareInfo.shareContent);
            share(activity, null, umVideo);
        }
    }

    public void share(Activity activity, UMusic music, UMVideo video) {
        if (getView() == null) {
            return;
        }

        UMShareListener shareListener = new UMShareListener() {
            /**
             * 分享开始的回调
             * @param platform 平台类型
             */
            @Override
            public void onStart(SHARE_MEDIA platform) {

            }

            /**
             *  分享成功的回调
             * @param platform 平台类型
             */
            @Override
            public void onResult(SHARE_MEDIA platform) {
                if (platform == SHARE_MEDIA.QQ || platform == SHARE_MEDIA.QZONE) {
                    return;
                }
                Toast.makeText(activity, "分享成功", Toast.LENGTH_LONG).show();
            }

            /**
             * 分享失败的回调
             * @param platform 平台类型
             * @param t 错误原因
             */
            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                LogUtils.e(t);
            }

            /**
             *  分享取消的回调
             * @param platform 平台类型
             */
            @Override
            public void onCancel(SHARE_MEDIA platform) {
                if (platform == SHARE_MEDIA.QQ || platform == SHARE_MEDIA.QZONE) {
                    return;
                }
                Toast.makeText(activity, "取消分享", Toast.LENGTH_LONG).show();
            }
        };

        ShareBoardlistener shareBoardlistener = (snsPlatform, share_media) -> {
            if (share_media != null) {
                if (music != null) {
                    new ShareAction(activity)
                            .setPlatform(share_media)
                            .withMedia(music)
                            .setCallback(shareListener)
                            .share();
                } else if (video != null) {
                    new ShareAction(activity)
                            .setPlatform(share_media)
                            .withMedia(video)
                            .setCallback(shareListener)
                            .share();
                }

            }
        };

        new ShareAction(activity)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE, SHARE_MEDIA.WEIXIN)
                .setShareboardclickCallback(shareBoardlistener)
                .open();

        RxBus.get().post(RxBusEventType.Music.SHARE, RxbusListObject.instance(null));
    }
}
